matplotlib[03]python 绘制动画animation

摘要

一图胜千言,动画胜百图。python中的matplotlib可以快速绘制gif动画。

准备

安装matplotlib

python -m pip install matplotlib

安装 ffmpeg,为了保存gif图片

https://www.ffmpeg.org/download.html#build-windows

在win中如果保存gif出错,可以先保存为mp4,然后利用ffmpeg命令转化为gif动画。

H:\file> ffmpeg.exe -i .\line.mp4 line.gif

DEMO

  • 小球滚动动画
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#!python2
#coding: utf-8
import sys
import numpy as np
import pylab as plt
from matplotlib.animation import FuncAnimation
from matplotlib import animation
fig, ax = plt.subplots()
fig.set_tight_layout(True)
plt.rcParams['animation.ffmpeg_path'] =u'D:\\Python27\\Scripts\\ffmpeg.exe'
FFwriter = animation.FFMpegWriter()

fh=open("pmf.dat")
xlist=[]
ylist=[]
barlist=[]
xydict={}
for line in fh.readlines()[1:]:
x,y,bar=line.strip().split()
xydict[x]=y
x,y,bar=map(float,line.strip().split())
xlist.append(x)
ylist.append(y)

barlist.append(bar)

#http://www.tuicool.com/articles/Z7BzY3V



plt.plot(xlist,ylist)
#polygoncollection
point_line,=plt.plot(xlist[0],ylist[0],'or')
def update(i):
pass
point_line.set_xdata(xlist[i])
point_line.set_ydata(ylist[i])
return point_line
#http://ffmpeg.zeranoe.com/builds/
if __name__=='__main__':
anim=FuncAnimation(fig,update,frames=np.arange(0,99),interval=200)
# ffmpeg -i small.mp4 small.gif
# http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/
anim.save('line.mp4',writer=FFwriter )
anim.save('line.gif',writer=FFwriter )
# plt.show()

核心思想

matplotlib中的一切元素都是对象,设置动画的关键就是选择合适的对象。

  • 首先创建一张静态图,动画的第一帧
  • 在第一帧上面包含一个对象,通过设置该对象的属性可以实现动画

参考

  1. http://www.tuicool.com/articles/Z7BzY3V
  2. http://ffmpeg.zeranoe.com/builds/
  3. http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/

素材文件 pmf.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
x            y           bar
20.41195 2.74 0
20.78465 1.37592 0.7196
21.15736 0.76087 0.74207
21.53007 0.3116 0.72869
21.90277 0.05602 0.73386
22.27548 0.05348 0.75097
22.64819 0.08492 0.74599
23.02089 0.0985 0.71082
23.3936 0.06613 0.70322
23.7663 0.14481 0.69655
24.13901 0.3257 0.72263
24.51172 0.27692 0.72225
24.88442 0.25218 0.72751
25.25713 0.36809 0.74037
25.62984 0.55323 0.75481
26.00254 0.72995 0.74839
26.37525 0.94172 0.74698
26.74796 1.13001 0.73844
27.12066 1.2329 0.72841
27.49337 1.25246 0.72547
27.86608 1.22187 0.70788
28.23878 1.19993 0.70379
28.61149 1.06911 0.69691
28.98419 0.90244 0.68734
29.3569 0.89425 0.69907
29.72961 0.91524 0.71852
30.10231 0.75044 0.696
30.47502 0.58894 0.69201
30.84773 0.45604 0.67034
31.22043 0.42346 0.65023
31.59314 0.4405 0.65072
31.96585 0.33601 0.63975
32.33855 0.20371 0.62343
32.71126 0.17893 0.62964
33.08396 0.05549 0.61188
33.45667 -0.00328 0.60007
33.82938 0.12251 0.61449
34.20208 0.25589 0.62247
34.57479 0.42555 0.65859
34.9475 0.61604 0.64286
35.3202 0.614 0.64946
35.69291 0.57622 0.66402
36.06562 0.58349 0.65839
36.43832 0.58438 0.63197
36.81103 0.65653 0.65727
37.18373 0.83727 0.70999
37.55644 1.08881 0.72572
37.92915 1.60682 0.8742
38.30185 2.10805 0.84269
38.67456 2.42247 0.83927
39.04727 2.75899 0.81526
39.41997 3.09685 0.79483
39.79268 3.3616 0.78948
40.16539 3.61108 0.79816
40.53809 3.87369 0.7951
40.9108 4.0805 0.77054
41.28351 4.216 0.76996
41.65621 4.3389 0.77383
42.02892 4.44717 0.76177
42.40162 4.71326 0.74032
42.77433 5.11622 0.72166
43.14704 5.39028 0.71028
43.51974 5.48795 0.73882
43.89245 5.64299 0.7484
44.26516 5.76421 0.75824
44.63786 5.85875 0.75837
45.01057 6.02089 0.76564
45.38328 6.19278 0.75816
45.75598 6.26079 0.76254
46.12869 6.25071 0.75838
46.50139 6.23831 0.75017
46.8741 6.20682 0.75129
47.24681 6.24886 0.77286
47.61951 6.3017 0.76859
47.99222 6.43433 0.76982
48.36493 6.56277 0.77959
48.73763 6.59075 0.79015
49.11034 6.58024 0.7903
49.48305 6.56504 0.7857
49.85575 6.57508 0.77676
50.22846 6.61263 0.78518
50.60116 6.55328 0.77659
50.97387 6.52119 0.77264
51.34658 6.54181 0.77423
51.71928 6.53653 0.77963
52.09199 6.49281 0.78699
52.4647 6.44565 0.79181
52.8374 6.41955 0.79063
53.21011 6.40472 0.79415
53.58282 6.36367 0.8039
53.95552 6.36215 0.79789
54.32823 6.37414 0.80248
54.70094 6.36651 0.79711
55.07364 6.39756 0.7919
55.44635 6.4867 0.78849
55.81905 6.51402 0.79147
56.19176 6.48499 0.79197
56.56447 6.41461 0.79307
56.93717 6.26777 0.78993
57.30988 6.65838 0.72711